package pers.yurwisher.dota2.pudge.security.service.impl;

import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import pers.yurwisher.dota2.pudge.constants.CacheConstant;
import pers.yurwisher.dota2.pudge.enums.UserClientType;
import pers.yurwisher.dota2.pudge.security.CurrentUser;
import pers.yurwisher.dota2.pudge.security.JwtUser;
import pers.yurwisher.dota2.pudge.security.cache.OnlineUser;
import pers.yurwisher.dota2.pudge.security.service.IOnlineUserService;
import pers.yurwisher.dota2.pudge.system.service.CustomRedisCacheService;
import pers.yurwisher.dota2.pudge.utils.PudgeUtil;

import javax.servlet.http.HttpServletRequest;
import java.time.Instant;
import java.time.LocalDateTime;

/**
 * @author yq
 * @date 2020/09/18 17:20
 * @description 在线用户service
 * @since V1.0.0
 */
@Service
public class OnlineUserServiceImpl implements IOnlineUserService {

    private final CustomRedisCacheService customRedisCacheService;
    private static final Logger logger = LoggerFactory.getLogger(OnlineUserServiceImpl.class);

    public OnlineUserServiceImpl(CustomRedisCacheService customRedisCacheService) {
        this.customRedisCacheService = customRedisCacheService;
    }

    @Override
    public OnlineUser get(String token) {
        return customRedisCacheService.get(CacheConstant.Strings.ONLINE_USER, token);
    }

    @Override
    public void save(HttpServletRequest request, String token, JwtUser user) {
        PudgeUtil.UserClientInfo clientInfo = PudgeUtil.getUserClientInfo(request);
        UserClientType clientType = this.getType(clientInfo.getSystem());
        long onlineUserCacheExpireSeconds = getOnlineUserCacheExpireSeconds(clientType);
        //存入redis
        customRedisCacheService.set(CacheConstant.Strings.ONLINE_USER, token, () -> {
            OnlineUser onlineUser = new OnlineUser();
            CurrentUser currentUser = user.getUser();
            onlineUser.setUsername(currentUser.getUsername());
            onlineUser.setNickname(currentUser.getNickname());
            onlineUser.setToken(token);
            LocalDateTime now = LocalDateTime.now();
            onlineUser.setLoginDate(now);

            onlineUser.setIp(clientInfo.getIp());
            onlineUser.setAddress(clientInfo.getAddress());
            onlineUser.setBrowser(clientInfo.getBrowser());
            //客户端操作系统
            onlineUser.setType(StrUtil.isNotBlank(clientInfo.getSystem()) ? UserClientType.PC : null);
            //设置过期时间 方便后续续期
            onlineUser.setExpiredAt(Instant.now().getEpochSecond() + onlineUserCacheExpireSeconds);
            return onlineUser;
        }, onlineUserCacheExpireSeconds,null);
    }

    /**
     * todo 根据从请求头分析出的系统 得出对应的客户端类型
     *
     * @param system 系统
     * @return 客户端类型
     */
    private UserClientType getType(String system) {
        return UserClientType.PC;
    }

    /**
     * 根据不同客户端类型获取在线用户过期时间
     *
     * @param type 客户端类型
     * @return 过期时间
     */
    private long getOnlineUserCacheExpireSeconds(UserClientType type) {
        return customRedisCacheService.getCacheExpireSeconds(PudgeUtil.generateKeyWithDoubleColon(CacheConstant.Strings.ONLINE_USER, type.name()));
    }

}
